﻿using EasyToHelp.DBFactory;
using EasyToHelp.Entity;
using System;
using System.Collections.Generic;
using System.Text;

namespace EasyToHelp.Service.Service
{
    /// <summary>
    /// 商户操作
    /// </summary>
    public class MerchantInfoService : IMerchantInfoService
    {
        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(MerchantInfo model)
        {
            return DBHelper.InsertExecute<MerchantInfo>(model).Id;
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(MerchantInfo model)
        {
            return DBHelper.UpdateExecute<MerchantInfo>(model);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(MerchantInfo model)
        {
            return DBHelper.DeleteExecute<MerchantInfo>(model);
        }
        #endregion

        #region 获取所有
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<MerchantInfo> GetList()
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Query<MerchantInfo>().ToList();
            }
        }
        /// <summary>
        /// 获取营业执照信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public BusinessLicenseInfo GetLicenset(int UserId)
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                BusinessLicenseInfo newmodel = new BusinessLicenseInfo();
                var model= db.Query<MerchantInfo>().InnerJoin<BusinessLicenseInfo>((m, b) => m.Id == b.MerchantId).Select((m, b) => new { MerchantInfo = m, BusinessLicenseInfo = b }).Where(x => x.MerchantInfo.UserId == UserId).FirstOrDefault();
                if (model!=null)
                {
                    newmodel = model.BusinessLicenseInfo;
                }                
                return newmodel;
            }
        }
        #endregion

    }
}
